package cn.nawang.ebeim.client;

import java.io.File;
import java.util.Date;

import cn.nawang.ebeim.client.bean.RecoveryTask;
import cn.nawang.ebeim.client.bean.UserBehaviorsLog;
import cn.nawang.ebeim.client.util.CommonMethodsUtils;
import cn.nawang.ebeim.client.util.Constants;
import cn.nawang.ebeim.client.util.FileUtil;
import cn.nawang.ebeim.client.util.TransportUtil;
import cn.nawang.ebeim.client.util.UserBehaviorsUploadUtils;
import cn.nawang.ebeim.client.version.FileNode;
import cn.nawang.ebeim.client.version.Node;
import cn.nawang.ebeim.client.version.NodeOp;

public class FileRecoveryOp implements NodeOp {

    private RecoveryTask task;

    public FileRecoveryOp(RecoveryTask task) {
        this.task = task;
    }

    @Override
    public boolean doTask(Node node) {
        long beginTime = new Date().getTime();
        FileNode fNode = (FileNode) node;

        String signature = fNode.getInfo().getSignature();
        /** 通过数据源、签名下载数据文件 */
        boolean result = TransportUtil.downloadDataFile(task, signature,node);
        /** 生成文件默认的文件路径*/
        String fileAbsolutePath = node.getPath() + File.separator + node.getName();
        String taskRecoveryPath = task.getRecoveryPath();
        String srcDir = FileUtil.getDownloadDataFilePath(task.getDataSourceId());
        if (taskRecoveryPath == null) {
            taskRecoveryPath = srcDir;
        }
        //保险箱数据下载的主树点击节点
        String userClickNodePath = task.getUserClickNodePath();
        if(userClickNodePath!=null){
          int i = userClickNodePath.lastIndexOf("\\");
          if(i!=-1){
            userClickNodePath = userClickNodePath.substring(0,i);
          }
          int idx = fileAbsolutePath.indexOf(userClickNodePath);
          if(idx!=-1){
            fileAbsolutePath = fileAbsolutePath.substring(idx+userClickNodePath.length(),fileAbsolutePath.length());
          }
        }
        String dest = taskRecoveryPath + fileAbsolutePath;
        
        /** 记录日志*/
        UserBehaviorsUploadUtils.logToFile(task, dest, beginTime, UserBehaviorsLog.ACTION_DOWANLOAD,
                result ? UserBehaviorsLog.STATUS_SUCCESS : UserBehaviorsLog.STATUS_ERROR);
        if (!result) {
            return result;
        }

        
        String srcPath = srcDir + File.separator + signature;
        String temp = srcDir + File.separator + signature + ".tmp";
        
        /** 解密 */
        if(task.getIsEncrypt().equals(Constants.IS_ENCRYPT_YES)){
          CommonMethodsUtils.decrypt(srcPath, temp);
        }else{
          File srcFile = new File(srcPath);
          File tempFile = new File(temp);
          srcFile.renameTo(tempFile);
        }

        /** 将已经还原的文件移动到用户自定义的位置中 */

        /** 设置重命名规则 */
        dest = CommonMethodsUtils.existFileRename(dest, null);
        CommonMethodsUtils.copyFile(temp, dest);

        File f1 = new File(srcPath);
        File f2 = new File(temp);
        if (f1.exists()) {
            f1.delete();
        }
        if (f2.exists()) {
            f2.delete();
        }

        return true;

    }

}
